package com.mrs.utils;


import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * description: BeanCopyUtils
 * date: 2022/8/6 11:27
 * author: MR.孙
 */
public class BeanCopyUtils {

    private BeanCopyUtils() {
    }

    public static  <V> V  copyBean(Object source,Class<V> clazz){

        //创建目标对象(利用反射new一个实例)
        V result = null;
        try {
             result = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //属性拷贝
        BeanUtils.copyProperties(source,result);

        //返回结果
        return result;

    }

    public static <T,V>  List<V> copyBeanList(List<T> list, Class<V> clazz){

        return list.stream().map(o->{
            return copyBean(o,clazz);
        }).collect(Collectors.toList());

    }



    //测试
/*    public static void main(String[] args) {
        Article article = new Article();
        article.setId(1L);
        article.setTitle("ss");

        HotArticleVo hotArticleVo = copyBean(article, HotArticleVo.class);
        System.out.println(hotArticleVo);
    }*/
}
